﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace BuildUtility
{
    public static class DataAccess
    {
        #region Constants
        private static readonly string CREATE_TABLE_SCORES_TEXT;
        #endregion

        #region Constructors
        static DataAccess()
        {
            CREATE_TABLE_SCORES_TEXT =
                "CREATE TABLE Scores(\n" +
                "Id_Sc INTEGER,\n" +
                "UserName_Sc TEXT,\n" +
                "Score_Sc INTEGER,\n" +
                "Time_Sc INTEGER,\n" +
                "PRIMARY KEY (Id_Sc));";
        }
        #endregion

        #region Methods
        public static SQLiteConnection Connect(string pDatabaseFilePath)
        {
            string connectionString = string.Format("Data Source={0};Version=3;", pDatabaseFilePath);
            SQLiteConnection connection = new SQLiteConnection(connectionString);
            connection.Open();

            return connection;
        }

        public static void CreateUserDataTables(SQLiteConnection pConnection)
        {
            ExecuteNonQuery(pConnection, CREATE_TABLE_SCORES_TEXT);
        }

        private static void ExecuteNonQuery(
            SQLiteConnection pConnection,
            string pCommandText)
        {
            using (SQLiteCommand command = pConnection.CreateCommand())
            {
                command.CommandText = pCommandText;
                command.ExecuteNonQuery();
            }
        }

        public static void Disconnect(SQLiteConnection pConnection)
        {
            pConnection.Close();
        }
        #endregion
    }
}